package com.lookup.api.verticle;

import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.rxjava.core.AbstractVerticle;
import io.vertx.rxjava.core.eventbus.EventBus;
import io.vertx.rxjava.core.eventbus.MessageConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EventBusWorkerVerticle extends AbstractVerticle {

    public String eventAddress = "node1";

    private Logger logger = LoggerFactory.getLogger("init.server"
            + EventBusWorkerVerticle.class.getName());

    @Override
    public void start() throws Exception {

        EventBus eb = vertx.eventBus();
        MessageConsumer<String> consumer = eb.consumer(eventAddress);
        consumer.handler(message -> logger.info("received a message: " + message.body()));

        consumer.completionHandler(res -> {
            if (res.succeeded()) {
                logger.info(" handler registration has reached all nodes");
            } else {
                logger.info(" handler registration can`t  reach all nodes");
            }
        });
        DeliveryOptions options = new DeliveryOptions();
        eb.send(eventAddress, " EventBus  Commucation Ok", options);
    }
}
